package com.example.dormitorysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dormitorysystem.entity.Beds;
import com.example.dormitorysystem.entity.Rooms;
import com.example.dormitorysystem.mapper.BedsMapper;
import com.example.dormitorysystem.mapper.RoomsMapper;
import com.example.dormitorysystem.service.BedsService;
import com.example.dormitorysystem.service.RoomsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吉吉王国组
 * @since 2022-11-26
 */
@Service
public class RoomsServiceImpl extends ServiceImpl<RoomsMapper, Rooms> implements RoomsService {

    @Override
    public Rooms getRoomById(String id) {
        int roomId = Integer.parseInt(id);
        Rooms room = baseMapper.selectById(roomId);
        if (room.getIsValid() == 0 || room.getIsDel() == 1) return null;
        //返回这个房间号
        return room;
    }



    @Override
    public List<Rooms> getRoomListByBuildingId(Integer buildingId) {
        // TODO: 2022/11/28 设置查找表，根据buildingID进行查询
        QueryWrapper<Rooms> roomsQueryWrapper = new QueryWrapper<Rooms>();
        roomsQueryWrapper.eq("building_id",buildingId);
        List<Rooms> rooms = baseMapper.selectList(roomsQueryWrapper);
        return rooms;
    }


    @Autowired
    private BedsMapper bedsMapper;

    /**
     * 统计房间内可以居住的人数
     * @param roomId
     * @return
     */
    @Override
    public int getBedsCount(Integer roomId) {
        int ans = 0;
        QueryWrapper<Beds> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",roomId)
                    .eq("is_valid",1)
                    .eq("status",0);

        ans = bedsMapper.selectCount(queryWrapper);
        return ans;
    }
}
